﻿using System.Collections.Generic;
using System.Linq;

namespace OneWorks.Web.Common.ActionResults
{
    public class DataGridResult<T> : CustomResult<DataGridEntity<T>>
    {
        private readonly PagedResult<T> _pagedResult;
        private readonly IEnumerable<T> _list;

        public DataGridResult(PagedResult<T> pagedResult)
        {
            _pagedResult = pagedResult;
        }

        public DataGridResult(IEnumerable<T> list)
        {
            if (list == null)
            {
                list = new List<T>();
            }
            _list = list;
        }

      

        public override DataGridEntity<T> GetObject()
        {
            DataGridEntity<T> entity;
            if (_pagedResult == null)
            {
                entity = new DataGridEntity<T>
                   {
                       Rows = _list,
                       Total = _list.Count()
                   };
            }
            else
            {
                entity = new DataGridEntity<T>
                {
                    Rows = _pagedResult.Data,
                    Total = _pagedResult.TotalRecords
                };
            }

            return entity;
        }
    }
}
